layui.use(['layer', 'form', 'formX', 'setter', 'mynotice', 'admin', 'ax', 'util', 'dataGrid', 'element', 'laydate'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var formX = layui.formX;
    var setter = layui.setter;
    var mynotice = layui.mynotice;
    var admin = layui.admin;
    var $ax = layui.ax;
    var util = layui.util;
    var dataGrid = layui.dataGrid;
    var element = layui.element;
    var laydate = layui.laydate;
    
    for(var i = 0; i < 8; i++ ){
	    if(i == 0){
		    laydate.render({
		        elem: '#limittime',
		        range: true,
		        max: 0
	    	});
	    }else{
		    laydate.render({
		        elem: '#limittime' + i,
		        range: true,
		        max: 0
	    	});
    	}
    }
    
    var Manage = {
    	columnId: '',
    	ins: {},
    	dataGridRender:() => { // 渲染数据列表
    	    Manage.ins = dataGrid.render({
    	        elem: '#cl',  // 容器
    	        templet: '#reslist',  // 模板
    	        url: '/api/zp/res/list',  // 数据接口
    	        page: {limit: 10},  // 开启分页
    	        parseData: function(res){
	        		$('.red').text(res.count);
	        		return res;
	        	},
    	        done: function(res, curr, count){
    	        	$('.red').text(count);
    	        }
    	    });
            dataGrid.on('tool(cl)', function (obj) {
                var data = obj.data;
                if (obj.event === 'resdel') { // 删除
                	Manage.del(data);
                }else if(obj.event === 'edit'){ // 修改
                	Manage.edit(data);
                } else if (obj.event === 'publish') { // 发布
	                Manage.publish(data);
	            } else if (obj.event === 'withdraw') { // 撤回
	                Manage.withdraw(data);
            	} else if (obj.event === 'recovery') { // 恢复
               		Manage.recovery(data);
            	} 
            });
    	},
    	del: (data) => { // 删除
    		admin.open({
    			type: 1,
    			title: false,
    			offset: 'auto',
    			area: '550px',
    			content: '<article class="infomodel">'+
		    				'<svg t="1586507358558" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4993" width="60" height="60"><path d="M512 64C264.58 64 64 264.58 64 512s200.58 448 448 448 448-200.58 448-448S759.42 64 512 64z m0 752a36 36 0 1 1 36-36 36 36 0 0 1-36 36z m51.83-551.95L548 636a36 36 0 0 1-72 0l-15.83-371.95c-0.1-1.33-0.17-2.68-0.17-4.05a52 52 0 0 1 104 0c0 1.37-0.07 2.72-0.17 4.05z" p-id="4994" fill="#fb6161"></path></svg>'+
		    				'<p class="deleteDialog-title">提示</p>'+
		    				'<p class="deleteDialog-desc">删除后，相关数据将被清除，删除操作不可撤销，请谨慎操作</p>'+
		    				'<div class="deleteDialog-buttonContainer">'+
		    					'<button type="button" class="layui-btn layui-btn-primary" style="width: 100px;" ew-event="closeDialog">取消</button>'+
		    					'<button type="button" class="layui-btn" style="background-color: #fb6161;width: 100px;" id="delBtn">确定</button>'+
		    				'</div>'+			
	    				'</article>',
    			success: function(layero, dIndex){
    				$(layero).children('.layui-layer-content').css('overflow', 'visible');
    				$('#delBtn').on('click', function(){
    					mynotice.load('正在删除...');
                    	var ajax = new $ax('/api/zp/del/' + data.articleId, 'post', function (res) {
                    		Manage.ins.reload({page: {curr: $(".layui-laypage-em").next().html()}});
                    		mynotice.destroy();
                    		mynotice.success('删除成功');
                    		layer.close(dIndex);
    	                }, function (res) {
    	                	mynotice.destroy();
    	                	mynotice.error(res.msg);
    	                });
    	                ajax.start();
    				})
    				
    			}
    		})
    	},
    	edit: (data) => { // 修改
    		window.open("/res/manage/edit/" + data.articleId);    
    	},
    	publish: (data) => { // 发布
    		admin.open({
    			type: 1,
    			title: false,
    			offset: 'auto',
    			area: '550px',
    			content: '<article class="infomodel">'+
		    				'<svg t="1586507358558" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4993" width="60" height="60"><path d="M512 64C264.58 64 64 264.58 64 512s200.58 448 448 448 448-200.58 448-448S759.42 64 512 64z m0 752a36 36 0 1 1 36-36 36 36 0 0 1-36 36z m51.83-551.95L548 636a36 36 0 0 1-72 0l-15.83-371.95c-0.1-1.33-0.17-2.68-0.17-4.05a52 52 0 0 1 104 0c0 1.37-0.07 2.72-0.17 4.05z" p-id="4994" fill="#fb6161"></path></svg>'+
		    				'<p class="deleteDialog-title">提示</p>'+
		    				'<p class="deleteDialog-desc">立即发布这篇信息？</p>'+
		    				'<div class="deleteDialog-buttonContainer">'+
		    					'<button type="button" class="layui-btn layui-btn-primary" style="width: 100px;" ew-event="closeDialog">取消</button>'+
		    					'<button type="button" class="layui-btn" style="background-color: #fb6161;width: 100px;" id="pubBtn">确定</button>'+
		    				'</div>'+			
	    				'</article>',
    			success: function(layero, dIndex){
    				$(layero).children('.layui-layer-content').css('overflow', 'visible');
    				$('#pubBtn').on('click', function(){
    					mynotice.load('正在发布...');
                    	var ajax = new $ax('/api/zp/publish/' + data.articleId, 'post', function (res) {
                    		Manage.ins.reload({page: {curr: $(".layui-laypage-em").next().html()}});
                    		mynotice.destroy();
                    		mynotice.success('发布成功');
                    		layer.close(dIndex);
    	                }, function (res) {
    	                	mynotice.destroy();
    	                	mynotice.error(res.msg);
    	                });
    	                ajax.start();
    				})
    				
    			}
    		})
    	},
    	withdraw: (data) => { // 撤回
    		admin.open({
    			type: 1,
    			title: false,
    			offset: 'auto',
    			area: '550px',
    			content: '<article class="infomodel">'+
		    				'<svg t="1586507358558" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4993" width="60" height="60"><path d="M512 64C264.58 64 64 264.58 64 512s200.58 448 448 448 448-200.58 448-448S759.42 64 512 64z m0 752a36 36 0 1 1 36-36 36 36 0 0 1-36 36z m51.83-551.95L548 636a36 36 0 0 1-72 0l-15.83-371.95c-0.1-1.33-0.17-2.68-0.17-4.05a52 52 0 0 1 104 0c0 1.37-0.07 2.72-0.17 4.05z" p-id="4994" fill="#fb6161"></path></svg>'+
		    				'<p class="deleteDialog-title">确定设为仅我可见？</p>'+
		    				'<p class="deleteDialog-desc">设为仅我可见后该内容可以正常查看</p>'+
		    				'<div class="deleteDialog-buttonContainer">'+
		    					'<button type="button" class="layui-btn layui-btn-primary" style="width: 100px;" ew-event="closeDialog">取消</button>'+
		    					'<button type="button" class="layui-btn" style="background-color: #fb6161;width: 100px;" id="chBtn">确定</button>'+
		    				'</div>'+			
	    				'</article>',
    			success: function(layero, dIndex){
    				$(layero).children('.layui-layer-content').css('overflow', 'visible');
    				$('#chBtn').on('click', function(){
    					mynotice.load('正在处理...');
                    	var ajax = new $ax('/api/zp/withdraw/' + data.articleId, 'post', function (res) {
                    		Manage.ins.reload({page: {curr: $(".layui-laypage-em").next().html()}});
                    		mynotice.destroy();
                    		mynotice.success('已设为仅我可见');
                    		layer.close(dIndex);
    	                }, function (res) {
    	                	mynotice.destroy();
    	                	mynotice.error(res.msg);
    	                });
    	                ajax.start();
    				})
    				
    			}
    		})
    	},
    	recovery: (data) => { // 恢复
    		mynotice.load('正在处理...');
        	var ajax = new $ax('/api/zp/recovery/' + data.articleId, 'post', function (res) {
        		Manage.ins.reload({page: {curr: $(".layui-laypage-em").next().html()}});
        		mynotice.destroy();
        		mynotice.success('已成功设为公开');
            }, function (res) {
            	mynotice.destroy();
            	mynotice.error(res.msg);
            });
            ajax.start();
    	}
    	
    }
    
    Manage.dataGridRender();
    
    element.on('tab(resListTab)', function(data){
    	var id = $(this).data('id');
    	Manage.columnId = id;
    	var elem= 'cl' + id;
    	Manage.ins = dataGrid.render({
	        elem: '#' + elem,  // 容器
	        templet: '#reslist',  // 模板
	        where: {columnId: Manage.columnId},
	        url: '/api/zp/res/list',  // 数据接口
	        page: {limit: 10},  // 开启分页
	        parseData: function(res){
	        	$('.red').text(res.count);
	        	return res;
	        },
	        done: function(res, curr, count){
	        	
	        }
	    });
    	 /* item里面的lay-event的点击事件 */
        dataGrid.on('tool('+ elem +')', function (obj) {
            var data = obj.data;
            if (obj.event === 'resdel') { // 删除
                Manage.del(data);
            } else if (obj.event === 'edit') { // 修改
                Manage.edit(data);
            } else if (obj.event === 'publish') { // 发布
                Manage.publish(data);
            } else if (obj.event === 'withdraw') { // 撤回
                Manage.withdraw(data);
            } else if (obj.event === 'recovery') { // 恢复
                Manage.recovery(data);
            }  
        });
	});
    
    form.on('submit(userSearchSubmit)', function(data){
    	data.field.columnId = Manage.columnId;
    	var limittime = $('#limittime' + Manage.columnId).val();
        if (limittime != '') {
            var limittimes = limittime.split(' - ');
            data.field.beginTime = limittimes[0];
            data.field.endTime = limittimes[1];
        }else{
        	data.field.beginTime = '';
            data.field.endTime = '';
        }
        console.log(Manage.ins);
    	Manage.ins.reload({where: data.field });
    	return false;
    })
    
   	util.event('lay-event', {
   		filter: function(){
   			$('.selected').removeClass('selected');
   			$(this).addClass('selected');
   			var data = {};
   			var limittime = $('#limittime').val();
   			
   			var articleType = $('#articleType' + Manage.columnId).val();
   			if(articleType){
   				data['articleType'] = articleType;
   			}
   			var formatId = $('#formatId' + Manage.columnId).val();
   			if(formatId){
   				data['formatId'] = formatId;
   			}
   			var serviceItems = $('#serviceItem' + Manage.columnId).val();
   			if(serviceItems){
   				data['formatId'] = formatId;
   			}
   			var shopId = $('#shopId' + Manage.columnId).val();
   			if(shopId){
   				data['formatId'] = shopId;
   			}
   			var title = $('#title' + Manage.columnId).val();
   			if(title){
   				data['title'] = title;
   			}
   			var limittime = $('#limittime' + Manage.columnId).val();
   			
            if (limittime) {
                var limittimes = limittime.split(' - ');
                data['beginTime'] = limittimes[0];
                data['endTime'] = limittimes[1];
            } else {
                data['beginTime'] = '';
                data['endTime'] = '';
            }
   			data['columnId'] = Manage.columnId;
   			data['draft'] = $(this).data('id');
    		Manage.ins.reload({where: data });
   		}
   	})
    
});